﻿//    Copyright 2010,2011 Himanshu Vasishth
//   
//    This file is part of SecretVault.
//
//    SecretVault is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    SecretVault is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with SecretVault.  If not, see <http://www.gnu.org/licenses/>.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace FileStoreProvider
{
    public class EncryptionUtilities
    {
        public static MemoryStream GetEncryptedStream(string masterPassword, Stream unencryptedStream)
        {
            byte[] masterPasswordHash = GetPasswordHash(masterPassword);

            byte[] key = masterPasswordHash;
            byte[] iv = masterPasswordHash;

            var rijndaelManaged = new RijndaelManaged();

            using (var memStream = new MemoryStream())
            {
                using (var cryptoStream = new CryptoStream(memStream,
                                                           rijndaelManaged.CreateEncryptor(key, iv),
                                                           CryptoStreamMode.Write))
                using (var streamWriter = new StreamWriter(cryptoStream))
                {
                    var data = new byte[unencryptedStream.Length];
                    unencryptedStream.Seek(0, SeekOrigin.Begin);
                    unencryptedStream.Read(data, 0, data.Length);

                    streamWriter.Write(Convert.ToBase64String(data));
                }
                // crate a new stream with the data since cryptstream will dispose the
                // underlying stream

                return new MemoryStream(memStream.ToArray());
            }
        }

        public static Stream DecryptStream(string masterPassword, Stream encryptedStream)
        {
            byte[] masterPasswordHash = GetPasswordHash(masterPassword);

            byte[] key = masterPasswordHash;
            byte[] iv = masterPasswordHash;
            var rijndaelManaged = new RijndaelManaged();

            using (var cryptoStream = new CryptoStream(encryptedStream,
                                            rijndaelManaged.CreateDecryptor(key, iv),
                                            CryptoStreamMode.Read))
            {
                var streamReader = new StreamReader(cryptoStream);
                return new MemoryStream(Convert.FromBase64String(streamReader.ReadToEnd()));
            }
        }

        private static byte[] GetPasswordHash(string masterPassword)
        {
            var md5Hash = MD5.Create();
            return md5Hash.ComputeHash(Encoding.Unicode.GetBytes(masterPassword));
        }
    }
}
